package com.bwie.a0522_chenzihang;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.TextView;

public class HomeActivity extends AppCompatActivity {
    TextView tv;
    int time = 5;
    SharedPreferences preferences;//存储价值对数据
    private SharedPreferences.Editor edit;
    @SuppressLint("HandlerLeak")
    Handler handler = new Handler() {
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            if (msg.what == 1) {
                time--;
                if (time == 0) {
                    Intent intent = new Intent();
                    intent.setClass(HomeActivity.this, MainActivity.class);
                    startActivity(intent);
                    finish();
                } else {
                    tv.setText(time + "");
                    handler.sendEmptyMessageDelayed(1, 1000);
                }
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);
        tv = findViewById(R.id.main_tv);
        handler.sendEmptyMessageDelayed(1, 1000);
        preferences = getSharedPreferences("home_pref", MODE_PRIVATE);
        edit = preferences.edit();//写入数据的对象
    }
}